﻿#include "editor/inspector.h"
#include "editor/drawing/tool.h"
#include "vg/stroke.h"

using namespace anip;

bool editor::InspectorContentProvider::tellInspectorAddContent()
{
    if (Globals::inspector == nullptr) return false;
    if (Globals::inspector->_hasContent(*this)) return false;
    Globals::inspector->_addContent(*this);
    return true;
}

bool editor::InspectorContentProvider::tellInspectorRemoveContent()
{
    if (Globals::inspector == nullptr) return false;
    if (!Globals::inspector->_hasContent(*this)) return false;
    Globals::inspector->_removeContent(*this);
    return true;
}

editor::Inspector::Inspector(Container* parent) : 
    EditorWindow(parent, { 200, 600 }, L"细节设定")
{
    assert(Globals::inspector == nullptr);
    Globals::inspector = this;

    auto btn = ui::FlowHelper::button(*this, L"Toggle debug display");
    btn->layoutOptions.stretchX = 1;
    btn->onClick = [](ui::ButtonBase& self) 
    {
        vg::Stroke::RenderDebug = !vg::Stroke::RenderDebug;
    };
    ui::FlowHelper::endRow(*this);

    for (auto tool : Globals::drawingTools)
    {
        if (tool->isActive())
            _addContent(*tool);
    }
}

void editor::Inspector::processMarkedDelete()
{
    while (!_panels.empty())
        _removeContent(*_panels.begin()->first);
    Globals::inspector = nullptr;
}

void editor::Inspector::_addContent(InspectorContentProvider& p)
{
    assert(_panels.find(&p) == _panels.end());
    auto panel = ui::FlowHelper::foldablePanel(*this, p.name());
    _panels.insert({ &p, panel });
    p.processCreateUI(*panel);
}

void editor::Inspector::_removeContent(InspectorContentProvider& p)
{
    assert(_panels.find(&p) != _panels.end());
    p.processDeleteUI(*_panels[&p]);
    _panels[&p]->markDelete();
    _panels.erase(&p);
}